package com.grandtech.insurance.service.impl;

import com.grandtech.insurance.dao.*;
import com.grandtech.insurance.domain.*;
import com.grandtech.insurance.service.DbBhjgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DbBhjgServiceImpl implements DbBhjgService {

    @Autowired
    private DbBhjgDAO dbBhjgDAO;

    @Autowired
    private DbKhxxDAO dbKhxxDAO;

    @Autowired
    private DbKhdkxxDAO dbKhdkxxDAO;

    @Autowired
    private DbBhjgDkDAO dbBhjgDkDAO;

    @Autowired
    private DbZzjgDAO dbZzjgDAO;


    @Override
    public BhEntity gettime(Map map) {
        return dbBhjgDAO.gettime(map);
    }

    @Override
    public List<DbBhjg> pagelist(Map map) {
        return dbBhjgDAO.pagelist(map);
    }

    @Override
    public int pagelistcount(Map map) {
        return dbBhjgDAO.pagelistcount(map);
    }

    @Override
    public List<DbBhjg> zypagelist(Map map) {
        return dbBhjgDAO.zypagelist(map);
    }

    @Override
    public int zypagelistcount(Map map) {
        return dbBhjgDAO.zypagelistcount(map);
    }

    @Override
    public List<String> timelist(Map map) {
        return dbBhjgDAO.timelist(map);
    }

    @Override
    public BhDtEntity1 getBhDtEntity1(BhDtEntity1 param) {

        //查询用户信息
        DbKhxx canshu1 = new DbKhxx();
        canshu1.setIdcode(param.getIdcode());
        canshu1.setXmbm(param.getXmbm());
        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(canshu1);
        if(dbKhxx!=null){
            param.setName(dbKhxx.getName());
            param.setXnzt(dbKhxx.getXnzt());
        }
        //统计地块数量和面积
        List<DbKhdkxx> khdkxxList = dbKhdkxxDAO.getKhdkxxByIdcodeAndXmbm(canshu1);
        int dbzzdksl = 0;
        BigDecimal dbzzmj = new BigDecimal(0);
        if(khdkxxList.size()>0){
            for(DbKhdkxx khdkxx : khdkxxList){
                if("01".equals(khdkxx.getZytype())){
                    dbzzdksl++;
                    dbzzmj = dbzzmj.add(khdkxx.getMj());
                }
            }
        }
        param.setDksl(dbzzdksl);
        param.setMj(dbzzmj);
        param.setMj1(String.format("%.2f", dbzzmj));
        //处理时间格式
        //param.setTime(param.getTime().replace("-","/"));
        //查询列表
        List<DbBhjg> list = dbBhjgDAO.getlist(param);
        if(list.size()>3){
            List<DbBhjg> list1 = new ArrayList<DbBhjg>();
            int n1 = 0;
            int nn = list.size()-1;
            int n = 0;
            for(int j=0;j<list.size();j++){
                if(param.getTime().equals(list.get(j).getTime()) && param.getZztype().equals(list.get(j).getZztype())){
                    n = j;
                    list.get(j).setGl(1);
                }
            }
            if(n==n1){
                list1.add(list.get(n1));
                list1.add(list.get(n1+1));
                list1.add(list.get(n1+2));
            }else if(n==nn){
                list1.add(list.get(nn-2));
                list1.add(list.get(nn-1));
                list1.add(list.get(nn));
            }else{
                list1.add(list.get(n-1));
                list1.add(list.get(n));
                list1.add(list.get(n+1));
            }
            param.setList(list1);
        }else{
            if(list.size()>0){
                for(DbBhjg d : list){
                    if(param.getTime().equals(d.getTime()) && param.getZztype().equals(d.getZztype())){
                        d.setGl(1);
                    }
                }
            }
            param.setList(list);
        }

        if(param.getList().size()>0){
            for(DbBhjg z : param.getList()){
                if(z.getSjmj()!=null){
                    z.setSjmj1(String.format("%.2f", z.getSjmj()));
                }else{
                    z.setSjmj1("0.00");
                }
                if(z.getWzzmj()!=null){
                    z.setWzzmj1(String.format("%.2f", z.getWzzmj()));
                }else{
                    z.setWzzmj1("0.00");
                }
            }
        }

        return param;
    }

    @Override
    public BhDtEntity2 getBhDtEntity2(BhDtEntity2 param) {

        //查询用户信息
        DbKhxx canshu1 = new DbKhxx();
        canshu1.setIdcode(param.getIdcode());
        canshu1.setXmbm(param.getXmbm());
        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(canshu1);
        if(dbKhxx!=null){
            param.setName(dbKhxx.getName());
            param.setXnzt(dbKhxx.getXnzt());
            param.setQhdm(dbKhxx.getQhdm());
            param.setQhmc(dbKhxx.getQhmc());
        }
        //根据地块编码查询地块
        Map canshu2 = new HashMap();
        canshu2.put("dkbm",param.getDkbm());
        List<Map> dks = dbKhdkxxDAO.getDksjShape(canshu2);
        if(dks.size()>0){
            param.setMj(new BigDecimal(dks.get(0).get("mj").toString()));
            param.setMj1(String.format("%.2f", new BigDecimal(dks.get(0).get("mj").toString())));
        }
        //查询列表
        canshu2.put("dkbm",param.getDkbm());
        canshu2.put("idcode",param.getIdcode());
        canshu2.put("xmbm",param.getXmbm());
        canshu2.put("zztype",param.getZztype());
        canshu2.put("time",param.getTime());
        List<DbBhjgDk> list = dbBhjgDkDAO.getlist(canshu2);
        if(list.size()>0){
            for(DbBhjgDk z : list){
                z.setMj1(String.format("%.2f", z.getMj()));
                z.setSjmj1(String.format("%.2f", z.getSjmj()));
                z.setWzzmj1(String.format("%.2f", z.getWzzmj()));
            }
        }
        param.setList(list);

        return param;
    }

    @Override
    public int savejg(Dbzzjgs param) {
        return dbZzjgDAO.savejg(param);
    }
}








































